package com.zidian.leader.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.zidian.leader.R;
import com.zidian.leader.adapter.ChartOptionListAdapter;
import com.zidian.leader.api.HttpFactory;
import com.zidian.leader.common.activity.MySwipeBackActivity;
import com.zidian.leader.common.util.ColorConstants;
import com.zidian.leader.common.util.HawkUtils;
import com.zidian.leader.common.util.StringConstants;
import com.zidian.leader.entity.ColleagueEvaTwoIndex;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import rx.Subscriber;
import rx.functions.Func1;

/**
 * 同行/督导评价二级标签Activity
 * Created by GongCheng on 2017/1/12.
 */

public class ColleagueEvaTwoIndexActivity extends MySwipeBackActivity {
    @Bind(R.id.index_score_tv)
    TextView indexScoreTv;
    @Bind(R.id.colleague_two_index_bar_chart)
    BarChart colleagueTwoIndexBarChart;
    @Bind(R.id.eva_tag_rv)
    RecyclerView evaTagRv;

    private ChartOptionListAdapter adapter;
    private String indexName;
    private float indexScore;
    private String collegeName;
    private String statisticsType;
    private BarData barData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        indexName = intent.getStringExtra("indexName");
        indexScore = intent.getFloatExtra("indexScore", 0);
        collegeName = intent.getStringExtra("collegeName");
        statisticsType = intent.getStringExtra("statisticsType");
        initView();
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_colleague_eva_two_index;
    }

    private void initView() {
        //Toolbar
        actionBar.setTitle(indexName);
        actionBar.setHomeAsUpIndicator(R.drawable.ic_arrow_back_24dp);
        adapter = new ChartOptionListAdapter();
        evaTagRv.setLayoutManager(new LinearLayoutManager(this));
        evaTagRv.setAdapter(adapter);
        indexScoreTv.setText(indexScore + StringConstants.STRING_EMPTY);
        initBarChart(colleagueTwoIndexBarChart);
        getColleagueTwoIndex();
    }

    /**
     * 初始化条形图
     *
     * @param chart 条形图
     */
    private void initBarChart(BarChart chart) {
        chart.setDescription(null);
        //设置拦截所有触摸事件
        chart.setTouchEnabled(false);
        chart.setNoDataText(getString(R.string.no_data));
        XAxis xAxis = chart.getXAxis();
        xAxis.setDrawGridLines(false);
        xAxis.setLabelCount(5);
        xAxis.setTextSize(10f);
        //X轴Value设计
//        xAxis.setValueFormatter(valueFormatter);
        xAxis.setGranularity(1);
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        //Y轴左侧
        YAxis yl = chart.getAxisLeft();
        yl.setDrawAxisLine(true);
        yl.setDrawGridLines(false);
        yl.setAxisMinimum(0f); // this replaces setStartAtZero(true)
        //Y轴右侧
        YAxis yr = chart.getAxisRight();
        yr.setDrawAxisLine(true);
        yr.setDrawGridLines(false);
        yr.setAxisMinimum(0f);
        yr.setEnabled(false);
        chart.animateY(1500);
    }

    /**
     * 得到BarChart的data
     *
     * @return BarData
     */
    private BarData getBarData(List<ColleagueEvaTwoIndex.TwoIndexListBean> data) {

        List<BarEntry> barEntries = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            barEntries.add(new BarEntry(i + 1, data.get(i).getTwoIndexScore()));
        }
        BarDataSet dataSet = new BarDataSet(barEntries, "BarChart");
        dataSet.setColors(ColorConstants.CHART_COLORS);
        BarData barData = new BarData(dataSet);
        barData.setBarWidth(0.3f);
        barData.setValueTextSize(12);
        return barData;
    }

    private void getColleagueTwoIndex() {
        subscription = HttpFactory.getInstance().getColleagueEvaTwoIndex(
                collegeName, statisticsType, indexName, HawkUtils.getUsername(),
                HawkUtils.getRoleId() + StringConstants.STRING_EMPTY, HawkUtils.getToken())
                .map(new Func1<ColleagueEvaTwoIndex, List<String>>() {
                    @Override
                    public List<String> call(ColleagueEvaTwoIndex colleagueEvaTwoIndex) {
                        List<String> list = new ArrayList<>();
                        barData = getBarData(colleagueEvaTwoIndex.getTwoIndexList());
                        for (int i = 0; i < colleagueEvaTwoIndex.getTwoIndexList().size(); i++) {
                            list.add(colleagueEvaTwoIndex.getTwoIndexList().get(i).getTwoIndexName());
                        }
                        return list;
                    }
                }).subscribe(new Subscriber<List<String>>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        progressDialog.show();
                    }

                    @Override
                    public void onCompleted() {
                        progressDialog.dismiss();
                    }

                    @Override
                    public void onError(Throwable e) {
                        progressDialog.dismiss();
                        Snackbar.make(evaTagRv, e.getMessage(), Snackbar.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onNext(List<String> strings) {
                        adapter.setData(strings);
                        colleagueTwoIndexBarChart.setData(barData);
                    }
                });
    }
}
